package ar.pol;
  
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
  
public class Cube {
   private FloatBuffer vertexBuffer;  // Buffer for vertex-array
   private int numFaces = 6;
   
   private float[][] colors = {  // Colors of the 6 faces
      { 0.5f, 0.5f, 0.5f, 1.0f },  // 0. orange
      { 0.5f, 0.5f, 0.5f, 1.0f },  // 1. violet
      { 0.5f, 0.5f, 0.5f, 1.0f },  // 2. green
      { 0.5f, 0.5f, 0.5f, 1.0f },  // 3. blue
      { 0.5f, 0.5f, 0.5f, 1.0f },  // 4. red
      { 0.5f, 0.5f, 0.5f, 1.0f }   // 5. yellow
   };
   
   private float[] material_a = {0.11f, 0.06f, 0.11f, 1.0f};
   private float[] material_d = {0.43f, 0.47f, 0.53f, 1.0f};
   private float[] material_s = {0.33f, 0.33f, 0.52f, 1.0f};
   private float[] material_e = { 0.0f,  0.0f,  0.0f, 1.0f};
  
   private float[] vertices = {  // Vertices of the 6 faces
      // FRONT
      -0.5f, -0.5f,  0.5f,  // 0. left-bottom-front
       0.5f, -0.5f,  0.5f,  // 1. right-bottom-front
      -0.5f,  0.5f,  0.5f,  // 2. left-top-front
       0.5f,  0.5f,  0.5f,  // 3. right-top-front
      // BACK
       0.5f, -0.5f, -0.5f,  // 6. right-bottom-back
      -0.5f, -0.5f, -0.5f,  // 4. left-bottom-back
       0.5f,  0.5f, -0.5f,  // 7. right-top-back
      -0.5f,  0.5f, -0.5f,  // 5. left-top-back
      // LEFT
      -0.5f, -0.5f, -0.5f,  // 4. left-bottom-back
      -0.5f, -0.5f,  0.5f,  // 0. left-bottom-front 
      -0.5f,  0.5f, -0.5f,  // 5. left-top-back
      -0.5f,  0.5f,  0.5f,  // 2. left-top-front
      // RIGHT
       0.5f, -0.5f,  0.5f,  // 1. right-bottom-front
       0.5f, -0.5f, -0.5f,  // 6. right-bottom-back
       0.5f,  0.5f,  0.5f,  // 3. right-top-front
       0.5f,  0.5f, -0.5f,  // 7. right-top-back
      // TOP
      -0.5f,  0.5f,  0.5f,  // 2. left-top-front
       0.5f,  0.5f,  0.5f,  // 3. right-top-front
      -0.5f,  0.5f, -0.5f,  // 5. left-top-back
       0.5f,  0.5f, -0.5f,  // 7. right-top-back
      // BOTTOM
      -0.5f, -0.5f, -0.5f,  // 4. left-bottom-back
       0.5f, -0.5f, -0.5f,  // 6. right-bottom-back
      -0.5f, -0.5f,  0.5f,  // 0. left-bottom-front
       0.5f, -0.5f,  0.5f   // 1. right-bottom-front
   };
        
   // Constructor - Set up the buffers
   public Cube() {
      // Setup vertex-array buffer. Vertices in float. An float has 4 bytes
      ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
      vbb.order(ByteOrder.nativeOrder()); // Use native byte order
      vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float
      vertexBuffer.put(vertices);         // Copy data into buffer
      vertexBuffer.position(0);           // Rewind
   }
  
   // Draw the shape
   public void draw(GL10 gl) {
      gl.glFrontFace(GL10.GL_CCW);    // Front face in counter-clockwise orientation
      gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face
      gl.glCullFace(GL10.GL_BACK);    // Cull the back face (don't display)

      gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
      gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
      
      gl.glEnable(GL10.GL_COLOR_MATERIAL);
      gl.glMaterialf(GL10.GL_FRONT, GL10.GL_SHININESS, 5.0f);
      gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_AMBIENT , material_a, 0);
      gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_DIFFUSE , material_d, 0);
      gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_SPECULAR, material_s, 0);
      gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_EMISSION, material_e, 0);

      // Render all the faces
      for (int face = 0; face < numFaces; face++)
      {
         // Set the color for each of the faces
         gl.glColor4f(colors[face][0], colors[face][1], colors[face][2], colors[face][3]);
         // Draw the primitive from the vertex-array directly
         gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, face*4, 4);
      }
	  
      gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
      gl.glDisable(GL10.GL_CULL_FACE);
   }
}
